﻿#include "GUISolverDialog.h"
#include "ui_GUISolverDialog.h"

#include "FITK_Kernel/FITKCore/FITKActionOperator.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h" 
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>

namespace GUI
{
    GUISolverDialog::GUISolverDialog(Core::FITKActionOperator* oper, QWidget *parent) :
        GUIDialogBase(parent),
        _oper(oper),
        _ui(new Ui::GUISolverDialog)
    {
        _ui->setupUi(this);
        // 设置对话框关闭时自动删除
        setAttribute(Qt::WA_DeleteOnClose);
        //去掉问号
        Qt::WindowFlags flags = windowFlags();
        flags &= ~Qt::WindowContextHelpButtonHint;
        setWindowFlags(flags);
        //初始化
        this->init();
        //设置窗口自适应隐藏组件
        this->layout()->setSizeConstraint(QLayout::SetMinAndMaxSize);
    }

    GUISolverDialog::~GUISolverDialog()
    {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }

    void GUISolverDialog::init()
    {
        _ui->groupBox_Information->setVisible(false);
        //获取工作路径
        AppFrame::FITKAppSettings* settings = FITKAPP->getAppSettings();
        QString workDir;
        if (settings) workDir = settings->getWorkingDir();
        _ui->lineEdit_WorkDir->setPlaceholderText(workDir);
        _ui->lineEdit_SolveFile->setPlaceholderText("case");
        _ui->lineEdit_ResultFile->setPlaceholderText("result");
        //初始化求解类型
        _ui->comboBox_SolverType->addItem(tr("All"), "");
        _ui->comboBox_SolverType->addItem(tr("Intermediate Amount"), "-param");
    }

    void GUISolverDialog::on_pushButton_Cancel_clicked()
    {
        this->reject();
    }

    void GUISolverDialog::on_pushButton_OK_clicked()
    {
        //从界面获取数据
        QString workDir = _ui->lineEdit_WorkDir->text();
        QString solveFileName = _ui->lineEdit_SolveFile->text();
        QString resultFileName = _ui->lineEdit_ResultFile->text();
        QString csvFileName;
        if (workDir.isEmpty())
            workDir = _ui->lineEdit_WorkDir->placeholderText();
        if (solveFileName.isEmpty())
            solveFileName = _ui->lineEdit_SolveFile->placeholderText();
        if (resultFileName.isEmpty())
            resultFileName = _ui->lineEdit_ResultFile->placeholderText();

        if (!QFileInfo(workDir).isDir())
        {
            QMessageBox::warning(this, "", tr("The work folder does not exist !"), QMessageBox::Ok);
            return;
        }

        if (workDir.back() == '/' || workDir.back() == '\\')
        {
            solveFileName = QString("%1%2.xml").arg(workDir).arg(solveFileName);
            resultFileName = QString("%1%2.hdf5").arg(workDir).arg(resultFileName);
            csvFileName = QString("%1%2.csv").arg(workDir).arg("sea_parameter");
        }
        else
        {
            solveFileName = QString("%1/%2.xml").arg(workDir).arg(solveFileName);
            resultFileName = QString("%1/%2.hdf5").arg(workDir).arg(resultFileName);
            csvFileName = QString("%1/%2.csv").arg(workDir).arg("sea_parameter");
        }
        QString type = _ui->comboBox_SolverType->currentData().toString();
        //设置数据
        if (_oper)
        {
            _oper->setArgs("SolverType", type);
            _oper->setArgs("WorkDir", workDir);
            _oper->setArgs("SolverFile", solveFileName);
            _oper->setArgs("ResultFileFile", resultFileName);
            _oper->setArgs("CSVFileFile", csvFileName);
        }

        this->accept();
    }

    void GUISolverDialog::on_pushButton_WorkDir_clicked()
    {
        AppFrame::FITKAppSettings* settings = FITKAPP->getAppSettings();
        QString workDir;
        if (settings) workDir = settings->getWorkingDir();
        workDir = QFileDialog::getExistingDirectory(this, "", workDir);
        _ui->lineEdit_WorkDir->setText(workDir);
    }
}

